//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package admin_docker_template_api_serv

import (
	"context"

	"atomgit.com/openlinksaas/api-server/admin_api_serv/admin_common"
	"atomgit.com/openlinksaas/api-server/dao"
	"atomgit.com/openlinksaas/api-server/dao/docker_dao"
	"atomgit.com/openlinksaas/api-server/dao/fs_dao"
	"atomgit.com/openlinksaas/proto-gen-go.git/docker_template_api"
	"go.mongodb.org/mongo-driver/mongo"
)

func (apiImpl *DockerTemplateAdminImpl) AddImage(ctx context.Context, req *docker_template_api.AdminAddImageRequest) (*docker_template_api.AdminAddImageResponse, error) {
	sess, ok := dao.CacheDao.GetAdminSession(ctx, req.AdminSessionId)
	if !ok {
		return &docker_template_api.AdminAddImageResponse{
			Code:   docker_template_api.AdminAddImageResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	if !sess.AuthOk {
		return &docker_template_api.AdminAddImageResponse{
			Code:   docker_template_api.AdminAddImageResponse_CODE_NOT_AUTH,
			ErrMsg: "未授权会话",
		}, nil
	}
	adminUserCfg, err := admin_common.GetAdminUserConfig(ctx, apiImpl.adminCfg, sess.UserName)
	if err != nil {
		return nil, err
	}
	if !adminUserCfg.PermInfo.DockerTemplatePerm.UpdateApp {
		return &docker_template_api.AdminAddImageResponse{
			Code:   docker_template_api.AdminAddImageResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	//检查应用是否存在
	exist, err := docker_dao.AppDao.Exist(ctx, req.AppId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &docker_template_api.AdminAddImageResponse{
			Code:   docker_template_api.AdminAddImageResponse_CODE_NO_APP,
			ErrMsg: "应用不存在",
		}, nil
	}
	//检查文件是否存在
	dtFsId, err := fs_dao.GlobalFsDao.GetDockerTemplateFs(ctx)
	if err != nil {
		return nil, err
	}
	exist, err = fs_dao.FsFileDao.Exist(ctx, dtFsId, req.AppImage.ThumbFileId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &docker_template_api.AdminAddImageResponse{
			Code:   docker_template_api.AdminAddImageResponse_CODE_NO_FILE,
			ErrMsg: "缩略图不存在",
		}, nil
	}
	exist, err = fs_dao.FsFileDao.Exist(ctx, dtFsId, req.AppImage.RawFileId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &docker_template_api.AdminAddImageResponse{
			Code:   docker_template_api.AdminAddImageResponse_CODE_NO_FILE,
			ErrMsg: "原图不存在",
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = docker_dao.AppImageDao.Insert(sessCtx, &docker_dao.AppImageDbItem{
			AppId:       req.AppId,
			ThumbFileId: req.AppImage.ThumbFileId,
			RawFileId:   req.AppImage.RawFileId,
			Weight:      req.AppImage.Weight,
		})
		if err != nil {
			return nil, err
		}
		return &docker_template_api.AdminAddImageResponse{
			Code: docker_template_api.AdminAddImageResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*docker_template_api.AdminAddImageResponse), nil
}

func (apiImpl *DockerTemplateAdminImpl) RemoveImage(ctx context.Context, req *docker_template_api.AdminRemoveImageRequest) (*docker_template_api.AdminRemoveImageResponse, error) {
	sess, ok := dao.CacheDao.GetAdminSession(ctx, req.AdminSessionId)
	if !ok {
		return &docker_template_api.AdminRemoveImageResponse{
			Code:   docker_template_api.AdminRemoveImageResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	if !sess.AuthOk {
		return &docker_template_api.AdminRemoveImageResponse{
			Code:   docker_template_api.AdminRemoveImageResponse_CODE_NOT_AUTH,
			ErrMsg: "未授权会话",
		}, nil
	}
	adminUserCfg, err := admin_common.GetAdminUserConfig(ctx, apiImpl.adminCfg, sess.UserName)
	if err != nil {
		return nil, err
	}
	if !adminUserCfg.PermInfo.DockerTemplatePerm.UpdateApp {
		return &docker_template_api.AdminRemoveImageResponse{
			Code:   docker_template_api.AdminRemoveImageResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	//检查应用是否存在
	exist, err := docker_dao.AppDao.Exist(ctx, req.AppId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &docker_template_api.AdminRemoveImageResponse{
			Code:   docker_template_api.AdminRemoveImageResponse_CODE_NO_APP,
			ErrMsg: "应用不存在",
		}, nil
	}
	exist, err = docker_dao.AppImageDao.Exist(ctx, req.AppId, req.ThumbFileId, req.RawFileId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &docker_template_api.AdminRemoveImageResponse{
			Code:   docker_template_api.AdminRemoveImageResponse_CODE_NO_IMAGE,
			ErrMsg: "应用截图不存在",
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = docker_dao.AppImageDao.Remove(sessCtx, req.AppId, req.ThumbFileId, req.RawFileId)
		if err != nil {
			return nil, err
		}
		return &docker_template_api.AdminRemoveImageResponse{
			Code: docker_template_api.AdminRemoveImageResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*docker_template_api.AdminRemoveImageResponse), nil
}

func (apiImpl *DockerTemplateAdminImpl) SetImageWeight(ctx context.Context, req *docker_template_api.AdminSetImageWeightRequest) (*docker_template_api.AdminSetImageWeightResponse, error) {
	sess, ok := dao.CacheDao.GetAdminSession(ctx, req.AdminSessionId)
	if !ok {
		return &docker_template_api.AdminSetImageWeightResponse{
			Code:   docker_template_api.AdminSetImageWeightResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	if !sess.AuthOk {
		return &docker_template_api.AdminSetImageWeightResponse{
			Code:   docker_template_api.AdminSetImageWeightResponse_CODE_NOT_AUTH,
			ErrMsg: "未授权会话",
		}, nil
	}
	adminUserCfg, err := admin_common.GetAdminUserConfig(ctx, apiImpl.adminCfg, sess.UserName)
	if err != nil {
		return nil, err
	}
	if !adminUserCfg.PermInfo.DockerTemplatePerm.UpdateApp {
		return &docker_template_api.AdminSetImageWeightResponse{
			Code:   docker_template_api.AdminSetImageWeightResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}

	//检查应用是否存在
	exist, err := docker_dao.AppDao.Exist(ctx, req.AppId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &docker_template_api.AdminSetImageWeightResponse{
			Code:   docker_template_api.AdminSetImageWeightResponse_CODE_NO_APP,
			ErrMsg: "应用不存在",
		}, nil
	}
	exist, err = docker_dao.AppImageDao.Exist(ctx, req.AppId, req.AppImage.ThumbFileId, req.AppImage.RawFileId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &docker_template_api.AdminSetImageWeightResponse{
			Code:   docker_template_api.AdminSetImageWeightResponse_CODE_NO_IMAGE,
			ErrMsg: "应用截图不存在",
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx mongo.SessionContext) (interface{}, error) {
		err = docker_dao.AppImageDao.UpdateWeight(sessCtx, req.AppId, req.AppImage.ThumbFileId, req.AppImage.RawFileId, req.AppImage.Weight)
		if err != nil {
			return nil, err
		}
		return &docker_template_api.AdminSetImageWeightResponse{
			Code: docker_template_api.AdminSetImageWeightResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*docker_template_api.AdminSetImageWeightResponse), nil
}
